package com.nopay.dxdwphone.ui.ativity;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.nopay.dxdwphone.base.BaseActivity;
import com.nopay.dxdwphone.bean.response.GetRichTextResponse;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.ycbjie.webviewlib.utils.ToastUtils;
import java.util.ArrayList;
import java.util.List;
import com.nopay.dxdwphone.Constants;
import com.nopay.dxdwphone.R;
import com.nopay.dxdwphone.databinding.ActivityVideoCourseBinding;
import com.nopay.dxdwphone.ui.adapter.VideoCourseAdapter;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: 更新用户姓名
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class VideoCourseActivity extends BaseActivity<ActivityVideoCourseBinding> {

    private VideoCourseAdapter adapter;
    private List<GetRichTextResponse.Data> dataList = new ArrayList<>();

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(VideoCourseActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(v -> mSwipeBackHelper.backward());

        // 添加头
        SinaRefreshView headerView = new SinaRefreshView(VideoCourseActivity.this);
        headerView.setArrowResource(R.mipmap.arrow);
        headerView.setTextColor(0xff745D5C);
        binding.refreshVideo.setHeaderView(headerView);
        // 禁止上拉加载更多
        binding.refreshVideo.setEnableLoadmore(false);

        binding.rvVideo.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new VideoCourseAdapter(VideoCourseActivity.this, dataList);
        binding.rvVideo.setAdapter(adapter);

        binding.refreshVideo.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                // TODO 刷新
                api.doGetVideoCourse().enqueue(new Callback<GetRichTextResponse>() {
                    @Override
                    public void onResponse(@NonNull Call<GetRichTextResponse> call, @NonNull Response<GetRichTextResponse> response) {
                        dataList.clear();
                        if (response.body() != null) {
                            if (Constants.RESULT_OK.equals(response.body().getCode())) {
                                if(response.body().getData() != null) {
                                    dataList.addAll(response.body().getData());
                                }
                            } else {
                                ToastUtils.showRoundRectToast(response.body().getMsg());
                            }
                        }

                        binding.refreshVideo.finishRefreshing();
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(@NonNull Call<GetRichTextResponse> call, @NonNull Throwable t) {
                        dataList.clear();
                        adapter.notifyDataSetChanged();
                        binding.refreshVideo.finishRefreshing();
                        ToastUtils.showRoundRectToast(getActivity().getResources().getString(R.string.call_api_error));
                    }
                });
            }
        });

        binding.refreshVideo.startRefresh();
    }

    @Override
    public void onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        GSYVideoManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }
}
